/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.deram.ctin.common.error;

import com.deram.ctin.common.bean.Code;
import com.deram.ctin.common.consts.ErrorConsts;
import com.google.gson.Gson;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/27
 */
public class BusinessException extends RuntimeException {

    private Code code;

    public BusinessException(Code code){
        super(code.getMessage());
        this.code = new Code(code.getCode(), code.getMessage());
    }

    public BusinessException(Code code, String message){
        super(message);
        this.code = new Code(code.getCode(), message);
    }

    public BusinessException(String message){
        super(message);
        this.code = new Code(ErrorConsts.BUSINESS.getCode(), message);
    }

    public Code getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        Gson gson = new Gson();
        return gson.toJson(code);
    }
}
